<template>
  <div class="contain-article">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>数据看板</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button icon="el-icon-plus" size="small" type="primary" round @click="openDrawer('add')"> 添加面经 </el-button>
        </div>
      </template>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="stem" label="标题" width="350px"> </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞" width="100px"> </el-table-column>
        <el-table-column prop="views" label="浏览数" width="100px"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <!-- 默认作用域插槽，row 可以拿到你插入的当前行数据 -->
            <div class="option">
              <i class="el-icon-view" @click="openDrawer('view', row.id)"></i>
              <i class="el-icon-edit-outline" @click="openDrawer('edit', row.id)"></i>
              <i class="el-icon-delete" @click="remove(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total="total" :page-size="query.pageSize" :page-count="query.current" @current-change="currentChange"> </el-pagination>
    </el-card>
    <el-drawer :title="title" :visible.sync="drawer" :direction="direction" size="780px" :before-close="handleClose">
      <div class="view" v-if="drawerTitle === 'view'">
        <p>{{ form.stem }}</p>
        <hr />
        <p v-html="form.content"></p>
      </div>
      <el-form v-else :model="form" ref="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" placeholder="输入面经标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- contain 对应的是el-form表单中需要校验的标签prop属性值 -->
          <quillEditor v-model="form.content" @blur="$refs.form.validateField('content')"></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button @click="reset">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script>
import { adminInterviewQuery, adminInterviewCreate, adminInterviewRemove, adminInterviewShow, adminInterviewUpdate } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      tableData: [],
      total: 0,
      direction: 'rtl',
      drawer: false,
      drawerTitle: '',
      query: {
        current: 1,
        pageSize: 5
      },
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '标题不能为空', tirgger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', tirgger: 'blur' }]
      }
    }
  },
  created() {
    this.getData()
  },
  computed: {
    title() {
      if (this.drawerTitle === 'add') {
        return '添加面经'
      } else if (this.drawerTitle === 'view') {
        return '查看面经'
      } else if (this.drawerTitle === 'edit') {
        return '修改面经'
      } else {
        // 去你m的g语法
        return ''
      }
    }
  },
  methods: {
    async getData() {
      const res = await adminInterviewQuery(this.query)
      console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    currentChange(pages) {
      console.log(pages)
      this.query.current = pages
      this.getData()
    },
    async openDrawer(type, id) {
      console.log(type, id)
      this.drawer = true
      this.drawerTitle = type
      // 点击查看按钮 和编辑按钮 我们都要让数据进行回显
      if (type !== 'add') {
        const { data: res } = await adminInterviewShow(id)
        console.log(res)
        this.form = res
        console.log(this.form)
      }
    },
    handleClose() {
      // 这个方法在你关闭 抽屉组件时生效 我们让输入框数据重置,并将drawer的值改为false
      if (this.drawerTitle !== 'view') {
        // 查看数据时是没有表单的所以不需要重置
        this.$refs.form.resetFields()
      }
      // this.$message.warning('fsdfsfsdf')
      this.drawer = false
    },
    async submit() {
      // 校验表单数据 不传参数返回的是一个 promise 成功为true 不成功报错 await 让异步代码同步执行
      // 这里我们可以如果数据校验不成功，或者接口请求失败可以进行一个错误捕获
      try {
        await this.$refs.form.validate()
        //  由于 form 的值被修改覆盖了 ，然而这个接口里面 只需要 stem content 这两个字段 所以我们要在这里解构 form
        if (this.drawerTitle === 'add') {
          // 如果当前是添加面经，才调用这个接口
          const { stem, content } = this.form
          const res = await adminInterviewCreate({ stem, content })
          console.log(res)
          console.log(this.form)
          this.$message.success('添加成功')
        }
        // 修改回显后的数据 先判断是不是修改页面，在调用接口进行修改
        if (this.drawerTitle === 'edit') {
          // 添加成功后 应该让页码变为1  然后重新调用一下接口 再让抽屉框隐藏
          // this.$message.success('hhhhhhhhhhhhhhhhhhhhhh')
          const { id, stem, content } = this.form
          const str = await adminInterviewUpdate({ id, stem, content })
          console.log('fhdhfhdfd' + str)
        }

        this.query.current = 1
        this.getData()
        this.drawer = false
      } catch (error) {
        console.log(error)
        if (error.content[0].message) {
          this.$message.error(error.content[0].message)
        }
        if (error.stem[0].message) {
          this.$message.error(error.stem[0].message)
        }
      }
    },
    remove(id) {
      // 删除接口
      adminInterviewRemove(id)
      this.$message.success('删除成功')
      this.getData()
    },
    reset() {
      // this.$message.success('sdfsdfs')
      // 对整个表单项进行重置
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.contain-article {
  .el-card {
    margin-top: 25px;
    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-right: 16px;
    }
    .actions {
      font-size: 18px;
      display: flex;
      justify-content: space-around;
      color: #666;
      > i:hover {
        color: rgba(114, 124, 245, 1);
        cursor: pointer;
      }
    }
  }
  .el-pagination {
    margin-top: 20px;
    text-align: center;
  }
  .el-breadcrumb {
    margin-top: 10px;
  }
  .el-form {
    padding-right: 40px;
  }
  .quill-editor {
    ::v-deep .ql-editor {
      height: 300px;
    }
  }
  .el-rate {
    padding: 10px 0;
  }
  .article-preview {
    padding: 0 40px 40px 40px;
    > h5 {
      font-size: 20px;
      color: #666;
      border-bottom: 1px dashed #ccc;
      padding-bottom: 30px;
      margin: 0 0 20px 0;
    }
  }
  .option {
    i {
      margin: 0 15px;
      cursor: pointer;
      font-size: 20px;
    }
  }
  .view {
    margin: 25px;
    hr {
      border: 1px dashed #ccc;
    }
  }
}
</style>
